package com.android.lyricshow.service;

import java.util.ArrayList;
import java.util.List;

import com.android.lyricshow.bean.SongBean;
import com.android.lyricshow.net.DownLoadLRC;
import com.android.lyricshow.until.AppUntil;
import com.android.lyricshow.until.KEY;
import com.android.lyricshow.until.LogPrint;
import com.android.lyricshow.until.OP;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

public class PlayService extends Service implements PlayListener{

	private PlayManager mPM 		= null;
	
	private boolean mIsDownLoading	= false;
	
	private OperateBinder mOB		= null;
	
	@Override
    public void onCreate() {
        super.onCreate();
        
        mOB	= new OperateBinder(this);
        
        mPM = new PlayManager();
        mPM.setPlayListener(this);
    }
    
    @SuppressWarnings("unchecked")
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	
    	if(intent == null ){
    		return START_STICKY;
    	}
		
    	Bundle bd = intent.getExtras();
    	if(bd == null){
    		return START_STICKY;
    	}
    			
		if(bd.containsKey(KEY.SONG_BEAN_LIST)){
			mPM.setPlayList((ArrayList<SongBean>)bd.getSerializable(KEY.SONG_BEAN_LIST));
			mPM.setIndex(bd.getInt(KEY.CURRENT_INDEX, 0));
			
			mPM.startPlay();
		}else if(bd.containsKey(KEY.NOW_PLAYING)){
			mPM.notifyCurPlay();
		}
		
        return START_STICKY;
    }
    
	@Override
	public IBinder onBind(Intent it) {
		return mOB;
	}
	
	@Override
	public void onDestroy(){
		mPM.release();
		mPM = null;
	}
	
	@Override
	public void Error(int code) {
		
		notify(OP.PLAY_ERROR, null);
		LogPrint.d("ErrorCode:" + code);
	}
	
	@Override
	public void playInfo(int code, Intent intent) {
		notify(code, intent);
	}
	
	public void downLoad(SongBean sbean){
		if(mIsDownLoading){
			LogPrint.d("Is Downloading...");
			return;
		}
		
		mIsDownLoading = true;
		new DownLoad(this, sbean).start();
	}
	
	//Send information to UI
	public void notify(int code, Intent it){
		
		LogPrint.d("Notify: " + code);
		
		Intent intent = new Intent();
		intent.setAction(KEY.PLAY_RECEIVER);
		
		intent.putExtra(KEY.NOTIFY, code);
		if(it != null)
			intent.putExtras(it);
		
		sendBroadcast(intent);
	}
	
	class DownLoad extends Thread{
		
		private SongBean mSBean;
		private PlayService mService;
		
		public DownLoad(PlayService service, SongBean sbean){
			mSBean	= sbean;
			mService= service;
		}
		
		@Override
		public void run(){
			DownLoadLRC dl = new DownLoadLRC();
			
			String path = dl.search(mSBean.getArtist(), mSBean.getTitle(), AppUntil.ROOT_PATH);
			if(path != null){
				Intent intent = new Intent();
				
				intent.putExtra(KEY.SONG_BEAN, mSBean);
				intent.putExtra(KEY.PATH, path);
				
				mService.notify(OP.DOWNLOAD_SUCCEED, intent);
			}else{
				mService.notify(OP.DOWNLOAD_FAILED, new Intent().putExtra(KEY.SONG_BEAN, mSBean));
			}
			LogPrint.d( path != null ? "Download succeed." : "Download failed.");
			mIsDownLoading = false;
		}
	}
	
	class OperateBinder extends Binder implements IService{
		
		private PlayService playservice;
		
		public OperateBinder(PlayService service){
			playservice = service;
		}
		
		@Override
		public void stop() {
			mPM.stop();
		}

		@Override
		public void pause() {
			mPM.pause();
		}

		@Override
		public void resume() {
			mPM.resume();
		}

		@Override
		public void next() {
			mPM.playNext();
		}

		@Override
		public void previous() {
			mPM.setMode(PlayManager.SHUFF);
		}

		@Override
		public void seekTo(int pos) {
			mPM.seekTo(pos);
		}

		@Override
		public int getCurPos() {
			return mPM.getCurPos();
		}

		@Override
		public void start() {
			mPM.startPlay();
		}

		@Override
		public void download(SongBean sbean) {
			playservice.downLoad(sbean);
		}
		
		@Override
		public List<SongBean> getPlayList() {
			return mPM.getPlayList();
		}

		@Override
		public void addToPlayList(SongBean abean) {
			mPM.addToPlayList(abean);
		}

		@Override
		public void addToPlayList(ArrayList<SongBean> lists) {
			mPM.addToPlayList(lists);
		}
	}
}
